package com.yun.controller;


import com.yun.pojo.Schedule;
import com.yun.service.DirectorService;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;


@Controller
public class ExcelController {

    @Autowired
    DirectorService directorService;

    @RequestMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("值班员工表");
        String[] headers = {"时间", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
        Row rowHeader = sheet.createRow(0);
        for (int cloNum = 0; cloNum < headers.length; cloNum++) {
            Cell cell = rowHeader.createCell(cloNum);
            RichTextString text = new HSSFRichTextString(headers[cloNum]);
            cell.setCellValue(text);
        }
        int rowNum = 1;
        List<Schedule> schedules = directorService.queryName();
        for (Schedule schedule : schedules) {
            Row rowBody = sheet.createRow(rowNum);
            rowBody.createCell(0).setCellValue(schedule.getTime());
            rowBody.createCell(1).setCellValue(schedule.getOday());
            rowBody.createCell(2).setCellValue(schedule.getTday());
            rowBody.createCell(3).setCellValue(schedule.getSday());
            rowBody.createCell(4).setCellValue(schedule.getWday());
            rowBody.createCell(5).setCellValue(schedule.getFday());
            rowBody.createCell(6).setCellValue(schedule.getXday());
            rowBody.createCell(7).setCellValue(schedule.getRday());
            rowNum++;
        }
        String filename = "值班安排.xls";
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }


    @RequestMapping("/Import")
    public String Import(Model model) throws Exception {
        String path = "C:\\Users\\Administrator\\Desktop\\";
        FileInputStream inputStream = new FileInputStream(path + "计划表.xls");
        Workbook workbook = new HSSFWorkbook(inputStream);
        Sheet sheet = workbook.getSheetAt(0);
        Row rowHeader = sheet.getRow(0);
        if (rowHeader != null) {
            int numberOfCells = rowHeader.getPhysicalNumberOfCells();
            for (int colNum = 0; colNum < numberOfCells; colNum++) {
                Cell cell = rowHeader.getCell(colNum);
                if (cell != null) {
                    cell.getStringCellValue();
                }
            }
        }
        String cellValue = "";
        List<Schedule> schedule = new ArrayList<>();
        int numberOfRows = sheet.getPhysicalNumberOfRows();
        for (int rowNum = 1; rowNum < numberOfRows; rowNum++) {
            Row rowBody = sheet.getRow(rowNum);
            List<String> list = new ArrayList<>();
            if (rowBody != null) {
                int numberOfCells = rowBody.getPhysicalNumberOfCells();
                for (int colNum = 0; colNum < numberOfCells; colNum++) {
                    Cell cell = rowBody.getCell(colNum);
                    if (cell != null) {
                        cellValue = cell.getStringCellValue();
                        list.add(cellValue);
                    }
                }
            }
            System.out.println(list);
            Schedule schedule1 = new Schedule();
            schedule1.setTime(list.get(0));
            schedule1.setOday(list.get(1));
            schedule1.setTday(list.get(2));
            schedule1.setSday(list.get(3));
            schedule1.setWday(list.get(4));
            schedule1.setFday(list.get(5));
            schedule1.setXday(list.get(6));
            schedule1.setRday(list.get(7));
            schedule.add(schedule1);
        }
        System.out.println(schedule);
        model.addAttribute("schedules",schedule);
        return "director/watchList";
}
}
